/******************************************************************************
 * Copyright (c) 2008 Savino Sguera. All rights reserved. 
 *
 * This program and the accompanying materials are made available under the 
 * terms of the Eclipse Public License v1.0, which accompanies this 
 * distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
 *
 * Contributors:
 * 	Savino Sguera < s.sguera@ieee.org >
 ******************************************************************************
 */

package im.agents.users;


import im.agents.IMAgent;
import im.agents.contextmanager.GetAgentContextResponder;
import im.agents.utility.Utility;
import im.domain.Context;
import im.domain.MonitorData;
import im.domain.SLA;
import im.domain.XMLSerializer;

import java.util.*;

import jade.content.lang.Codec;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.Ontology;
import jade.core.*;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPANames;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPANames.InteractionProtocol;
import jade.domain.JADEAgentManagement.JADEManagementOntology;
import jade.lang.acl.ACLMessage;
import jade.proto.AchieveREInitiator;


/**
 * agente "finto", richiede un servizio
 * @author savino
 */
public class Requester extends IMAgent{
	
	
	private Map<String, SLA> subscribedSLAs = new HashMap<String,SLA>();
	//Context context;

	public void setup(){
		super.setup();
		
		
		this.doWait(2000);
		ACLMessage initiation = new ACLMessage(ACLMessage.PROPOSE);
		initiation.setSender(this.getAID());
		
		DFAgentDescription[] ds = findSLACheckers();
		if(ds== null || ds.length<=0){
			System.out.println("SC not found");
		}else {
			initiation.addReceiver(ds[0].getName());
		}
		
		/*MonitorData d = new MonitorData();
		d.setConversationId("id");
		d.getTempiInterarrivo().add(100);
		d.getTempiInterarrivo().add(1200);
		d.getTempiInterarrivo().add(1000);
		String modata = XMLSerializer.doSerialize(d);
		System.out.println(modata);
		MonitorData d2 = (MonitorData)XMLSerializer.doDeserialize(modata);
		for(Integer i : d2.getTempiInterarrivo()){
			System.out.print(i +" ");
		}*/
		
		SLA sla = packSLARequest();
		
		initiation.setContent(XMLSerializer.doSerialize(sla));
		initiation.setProtocol(InteractionProtocol.FIPA_PROPOSE);
		addBehaviour(new SLAProposeInitiator(this,initiation));
		addBehaviour(new GetAgentContextResponder(this));
		
		
	}

	private SLA packSLARequest() {
		SLA sla = new SLA();
		sla.setServizio("X");
		sla.setSubscriberA(getAID());
		sla.setTempoRispMax(1000);
		sla.setTassoArriviMax(0.1f);
		return sla;
	}
	
	private DFAgentDescription[] findSLACheckers() {
		DFAgentDescription[] ds = null;
		try {
			DFAgentDescription dfd = new DFAgentDescription();
			ServiceDescription s = new ServiceDescription ();
			s.setName("SLAChecker");
			s.setType("SLAChecker");
			dfd.addServices(s);
			ds = DFService.search(this, dfd);
			
		} catch (FIPAException e) {
			e.printStackTrace();
			return null;
		}
		return ds;
	}
	
	public void addSubscribedSLA(String service, SLA sla){
		subscribedSLAs.put(service, sla);
	}
	
	public SLA getSLASubscribeeForService(String service){
		if(!subscribedSLAs.containsKey(service)){
			return null;
		}
		return subscribedSLAs.get(service);
	}

	@Override
	public ServiceDescription getServiceDescription() {
		ServiceDescription service = new ServiceDescription();
		service.setName("requester");
		service.setType("requester");
		return service;
	}



}
